package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetMealController {
    @DubboReference
    private SetMealService setMealService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult page = setMealService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, page);
    }

    @GetMapping("findCheckGroupIds")
    public Result findCheckGroupIds(Integer id){
       List<Integer> checkgroups= setMealService.findCheckGroupIds(id);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkgroups);
    }

    @GetMapping("findById")
    public Result findById(Integer id){
        Setmeal serviceById = setMealService.findById(id);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,serviceById);
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //自己构造文件名称
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
        //将图片存入redis
        redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).add(fileName);
        try {
            qiniuUtils.upload2Qiniu(file.getInputStream(), fileName);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        String imgUrl = qiniuUtils.getUrlPrefix() + fileName;
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imgUrl);
    }

    @PostMapping("add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        String fileName = setmeal.getImg().replace(qiniuUtils.getUrlPrefix(), "");
        setmeal.setImg(fileName);
        setMealService.add(setmeal, checkgroupIds);
        redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).remove(setmeal.getImg());
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @PostMapping("edit")
    public Result edit(Integer[] checkgroupIds,@RequestBody Setmeal setmeal) {
        setMealService.edit(setmeal,checkgroupIds);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    @PostMapping("delete")
    public Result delete(Integer id) {
        setMealService.delete(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

}
